1.1. Data Types in C

Module 1.1 • Fundamentals of Data Types & Constants

A data type specifies the kind of data that a variable can store in memory. Different types of data require different amounts of memory and support different ranges of values.

When a variable is declared, its data type tells the compiler:

For example:

int age = 25;
float price = 199.99;
char grade = 'A';

In the above code:

Why Are Data Types Important?

Data types help to:

For example, storing a student's age as a character would not be appropriate:

char age = 20;   // Not recommended

Instead:

int age = 20;    // Correct

Basic Data Types in C

C provides four fundamental data types:

Data Type Description Example
charStores a single character'A'
intStores whole numbers100
floatStores decimal values with single precision25.75
doubleStores decimal values with higher precision3.1415926535

1. Integer Type (int)

The int data type is used to store whole numbers without decimal points.

Example

int students = 45;
int temperature = -10;

Output Values

45
-10

Common Uses

2. Character Type (char)

The char data type stores a single character enclosed within single quotes.

Example

char grade = 'B';
char symbol = '#';

Common Uses

3. Floating Point Type (float)

The float data type stores decimal numbers with moderate precision.

Example

float salary = 32500.75;
float temperature = 36.8;

Common Uses

4. Double Type (double)

The double data type stores decimal values with higher precision than float.

Example

double pi = 3.14159265358979;
double interestRate = 7.253468;

Common Uses

Type Modifiers

Type modifiers are used to change the size or range of basic data types. There are two categories:

1. Sign Modifiers

2. Size Modifiers

Signed and Unsigned Types

Signed

A signed variable can store both positive and negative values.

Example

signed int balance = -2500;

Possible Values

-2500
100
-50

Unsigned

An unsigned variable stores only zero or positive values.

Example

unsigned int population = 500000;

Since no memory is reserved for the sign, unsigned variables can store larger positive values.

Short and Long Types

Short Integer

Used when smaller integer values are expected.

Example

short year = 25;

Long Integer

Used when larger integer values are required.

Example

long distance = 987654321;

Common Data Types and Their Typical Sizes

Data Type Typical Size
char1 byte
short int2 bytes
int4 bytes
long int4 or 8 bytes
float4 bytes
double8 bytes
long double10 to 16 bytes

Note: Actual sizes may vary depending on the compiler and operating system.

Data Type Example Program

#include <stdio.h>
 
int main()
{
    int quantity = 15;
    float price = 249.50;
    double tax = 18.123456;
    char category = 'E';
 
    printf("Quantity = %d\n", quantity);
    printf("Price = %.2f\n", price);
    printf("Tax = %.6lf\n", tax);
    printf("Category = %c\n", category);
 
    return 0;
}

Output

Quantity = 15
Price = 249.50
Tax = 18.123456
Category = E

Constants in C

A constant is a fixed value that does not change during program execution.

Examples

100
25.75
'A'
"Hello"

Once defined, a constant remains unchanged throughout the program.

Types of Constants

C supports several types of constants:

1.1.1 Numeric Constants

Numeric constants represent numbers.

Rules

Valid Examples

250
-50
7890

Invalid Examples

12,500
35 10
4#5

Reason:

Integer Constants

Integer constants are whole numbers without decimal points.

There are three number systems used in C.

Decimal Constants (Base 10)

Uses digits 0–9.

Examples

25
1000
98765

Octal Constants (Base 8)

Uses digits 0–7 only. Must begin with 0.

Examples

012
075
0456

Invalid Example

089

Reason: Digit 8 is not allowed in octal numbers.

Hexadecimal Constants (Base 16)

Uses digits 0–9 and letters A–F. Must begin with 0x or 0X.

Examples

0x10
0x1F
0XABC
0x5D

Integer Constant Suffixes

Suffixes specify the type of an integer constant.

Examples

250U
5000L
75000UL

Meaning:

Real (Floating Point) Constants

Real constants contain a decimal point.

Examples

5.75
0.25
100.0
89.456

Scientific Notation

Very large or very small numbers can be written using exponential notation.

Examples

4.5e6
7.2e-4
1.25E8

Meaning:

4.5 × 10⁶
7.2 × 10⁻⁴
1.25 × 10⁸

Floating Point Suffixes

Examples

2.5F
3.14L

Meaning:

1.1.2 Character Constants

A character constant contains exactly one character enclosed in single quotes.

Valid Examples

'A'
'9'
'%'
'k'

Invalid Examples

'AB'
"X"
''

Reasons:

ASCII Values

Every character has a corresponding numeric value called an ASCII code.

Examples

Character ASCII Value
A65
B66
a97
b98
048
957

Example:

char letter = 'A';

Internally, C stores the value 65.

1.1.3 String Constants

A string constant is a sequence of characters enclosed in double quotes.

Examples

"Welcome"
"C Programming"
"2026"
"OpenAI"

Character vs String

'A' : This is a character constant.

"A" : This is a string constant.

The string actually contains:

A\0

where \0 is the null character added automatically by the compiler.

1.1.4 Symbolic Constants

Sometimes a constant value is used repeatedly throughout a program. Instead of writing the value multiple times, we can assign it a name. These are called symbolic constants.

Syntax

#define NAME value

Examples

#define MAX_STUDENTS 100
#define GST 18
#define PI 3.14159265
#define COMPANY "TechZone"

Example Program

#include <stdio.h>
 
#define PI 3.14159265
 
int main()
{
    float radius = 5;
 
    float area = PI * radius * radius;
 
    printf("Area = %.2f", area);
 
    return 0;
}

Advantages

Derived Data Types

Derived data types are built using basic data types. They include:

Array

Stores multiple values of the same type.

int marks[5] = {80, 75, 90, 85, 88};

Pointer

Stores the memory address of another variable.

int num = 10;
int *ptr = #

Structure

Groups variables of different data types under one name.

struct Student
{
    char name[30];
    int age;
};

Union

Allows different data types to share the same memory location.

union Data
{
    int number;
    float price;
};

Enumeration (enum)

An enumeration is a user-defined data type containing named integer constants.

Example

enum Month
{
    JAN,
    FEB,
    MAR
};

Void Data Type

The void data type represents the absence of a value.

Example

void displayMessage()
{
    printf("Welcome");
}

A function with void does not return any value.

Summary

Verify Comprehension: Technical Knowledge Assessment

Click your choice for each question to view feedback immediately. Complete all questions to evaluate your metric score.